import 'package:flutter/material.dart';
import '../widgets/platform/adaptive_card.dart';

/// 流程可视化组件
class ProcessVisualization extends StatelessWidget {
  final List<ProcessStep> steps;
  final String? title;
  final VoidCallback? onExpand;

  const ProcessVisualization({
    super.key,
    required this.steps,
    this.title,
    this.onExpand,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (title != null || onExpand != null) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (title != null)
                SelectableText(
                  title!,
                  style: Theme.of(context).textTheme.titleLarge,
                ),
              if (onExpand != null)
                TextButton(
                  onPressed: onExpand,
                  child: const SelectableText('展开流程图'),
                ),
            ],
          ),
          const SizedBox(height: 16),
        ],
        AdaptiveCard(
          child: Column(
            children: [
              // 步骤展示
              SizedBox(
                height: 80,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: steps.length,
                  itemBuilder: (context, index) {
                    final step = steps[index];
                    return _buildProcessStep(
                      context,
                      step,
                      index,
                      index == steps.length - 1,
                    );
                  },
                ),
              ),
              const SizedBox(height: 16),
              // 卡点信息
              if (steps.any((step) => step.isBottleneck)) ...[
                Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: Colors.red[50],
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: const Row(
                    children: [
                      Icon(Icons.warning, color: Colors.red, size: 16),
                      SizedBox(width: 8),
                      SelectableText(
                        '当前卡点：文档编写',
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 14,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ],
          ),
        ),
      ],
    );
  }

  /// 构建流程步骤
  Widget _buildProcessStep(
    BuildContext context,
    ProcessStep step,
    int index,
    bool isLast,
  ) {
    Color getStepColor() {
      if (step.isCompleted) return Theme.of(context).primaryColor;
      if (step.isActive) return Colors.orange;
      return Colors.grey;
    }

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 40,
              height: 40,
              decoration: BoxDecoration(
                color: getStepColor(),
                shape: BoxShape.circle,
              ),
              child: Center(
                child: SelectableText(
                  '${index + 1}',
                  style: const TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            const SizedBox(height: 4),
            SelectableText(
              step.title,
              style: TextStyle(
                fontSize: 12,
                color: getStepColor(),
              ),
            ),
          ],
        ),
        if (!isLast)
          Container(
            width: 40,
            height: 2,
            color:
                step.isCompleted ? Theme.of(context).primaryColor : Colors.grey,
          ),
      ],
    );
  }
}

/// 流程步骤数据模型
class ProcessStep {
  final String title;
  final bool isCompleted;
  final bool isActive;
  final bool isBottleneck;

  ProcessStep({
    required this.title,
    this.isCompleted = false,
    this.isActive = false,
    this.isBottleneck = false,
  });
}
